package com.frank.activedatabus.dispatch;

import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

import java.util.concurrent.ConcurrentHashMap;

public class DispatchManager {

    private ConcurrentHashMap<ThreadMode, IDispatch> dispatchMap = new ConcurrentHashMap<>();

    private DispatchManager() {

    }

    static class DisplayManagerHold {
        static DispatchManager instance = new DispatchManager();
    }

    public static DispatchManager getInstance() {
        return DisplayManagerHold.instance;
    }

    public synchronized IDispatch getDispatch(ThreadMode mode) {
        IDispatch dispatch = dispatchMap.get(mode);
        if (dispatch == null) {
            if (mode == ThreadMode.MAIN) {
                dispatch = new MainDispatch(new EventHandler(EventRunner.getMainEventRunner()));
            } else if (mode == ThreadMode.BACKGROUND) {
                dispatch = new BackgroudDispatch(new EventHandler(EventRunner.create("bgHandler")));
            } else {
                dispatch = new CurrentDispatch();
            }
            dispatchMap.put(mode, dispatch);
        }
        return dispatch;
    }


}
